﻿using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.W.realize;

namespace MyMES.Read.API.Application.CommendHandlers.W.realize
{
    public class MaterialproductclassificationCommandHanders : IRequestHandler<GetMaterialproductclassificationCommands, APIPaging<List<Materialproductclassification>>>
    {
        private readonly IBaseRepository<Materialproductclassification> baseRepository;

        public MaterialproductclassificationCommandHanders(IBaseRepository<Materialproductclassification> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        /// <summary>
        /// 显示物料产品分类信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIPaging<List<Materialproductclassification>>> Handle(GetMaterialproductclassificationCommands request, CancellationToken cancellationToken)
        {
            APIPaging<List<Materialproductclassification>> result = new APIPaging<List<Materialproductclassification>>();
            result.Code = APIEnum.查询成功;
            result.Msg = "查询成功";
            var sql = "select * from Materialproductclassification where IsDelete=false ORDER BY ParentId DESC";
            var list = await baseRepository.SelectAsync(sql);
            if (!string.IsNullOrEmpty(request.Classificationname))
            {
                list = list.Where(x => x.Classificationname.Contains(request.Classificationname)).ToList();
            }
            if (request.IsEnable != null)
            {
                var cao = request.IsEnable == 1 ? true : false;
                list = list.Where(x => x.IsEnable == cao).ToList();
            }
            result.TotalCount = list.Count;
            result.TotalPage= (int)Math.Ceiling(result.TotalCount*1.0/request.PageSize);
            list=list.Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToList();
            result.data = list;
            return result;
        }
    }
}
